# PyMite4Multiboot PortIO
# 
# Clone of the PortIO Python module by Fabrizio Pollastri for PyMite VM
# http://portio.inrim.it/

"""__NATIVE__
#include <sys/io.h>
"""


def outb(data, port):
    """__NATIVE__

    unsigned char data;
    unsigned short int port;

    pPmObj_t pdata;
    pPmObj_t pport;
    PmReturn_t retval = PM_RET_OK;

    /* If wrong number of args, raise TypeError */
    if(NATIVE_GET_NUM_ARGS() != 2)
    {
        PM_RAISE(retval, PM_RET_EX_TYPE);
        return retval;
    }

    /* If data is not an unsigned char, raise TypeError */
    pdata = NATIVE_GET_LOCAL(0);
    if(OBJ_GET_TYPE(pdata) != OBJ_TYPE_INT)
    {
        PM_RAISE(retval, PM_RET_EX_TYPE);
        return retval;
    }

    /* If port is not an unsigned short, raise TypeError */
    pport = NATIVE_GET_LOCAL(1);
    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
    {
        PM_RAISE(retval, PM_RET_EX_TYPE);
        return retval;
    }

    data = ((pPmInt_t)pdata)->val & 0xFF;
    port = ((pPmInt_t)pport)->val & 0xFFFF;

    outb(data,port);

    NATIVE_SET_TOS(PM_NONE);
    return retval;
    """
    pass


#def outw(data, port):
#    """__NATIVE__
#
#    unsigned short int data;
#    unsigned short int port;
#
#    pPmObj_t pdata;
#    pPmObj_t pport;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 2)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If data is not an unsigned char, raise TypeError */
#    pdata = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pdata) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(1);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    data = ((pPmInt_t)pdata)->val & 0xFFFF;
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    outw(data,port);
#
#    NATIVE_SET_TOS(PM_NONE);
#    return retval;
#    """
#    pass
#
#
#def outl(data, port):
#    """__NATIVE__
#
#    unsigned int data;
#    unsigned short int port;
#
#    pPmObj_t pdata;
#    pPmObj_t pport;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 2)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If data is not an unsigned char, raise TypeError */
#    pdata = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pdata) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(1);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    data = ((pPmInt_t)pdata)->val;
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    outl(data,port);
#
#    NATIVE_SET_TOS(PM_NONE);
#    return retval;
#    """
#    pass
#
#def outb_p(data, port):
#    """__NATIVE__
#
#    unsigned char data;
#    unsigned short int port;
#
#    pPmObj_t pdata;
#    pPmObj_t pport;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 2)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If data is not an unsigned char, raise TypeError */
#    pdata = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pdata) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(1);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    data = ((pPmInt_t)pdata)->val & 0xFF;
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    outb_p(data,port);
#
#    NATIVE_SET_TOS(PM_NONE);
#    return retval;
#    """
#    pass
#
#
#def outw_p(data, port):
#    """__NATIVE__
#
#    unsigned short int data;
#    unsigned short int port;
#
#    pPmObj_t pdata;
#    pPmObj_t pport;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 2)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If data is not an unsigned char, raise TypeError */
#    pdata = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pdata) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(1);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    data = ((pPmInt_t)pdata)->val & 0xFFFF;
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    outw_p(data,port);
#
#    NATIVE_SET_TOS(PM_NONE);
#    return retval;
#    """
#    pass
#
#
#def outl_p(data, port):
#    """__NATIVE__
#
#    unsigned int data;
#    unsigned short int port;
#
#    pPmObj_t pdata;
#    pPmObj_t pport;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 2)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If data is not an unsigned char, raise TypeError */
#    pdata = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pdata) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(1);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    data = ((pPmInt_t)pdata)->val;
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    outl_p(data,port);
#
#    NATIVE_SET_TOS(PM_NONE);
#    return retval;
#    """
#    pass


def inb(port):
    """__NATIVE__

    unsigned short int port;
    unsigned char data;

    pPmObj_t pport;
    pPmObj_t pdata;
    PmReturn_t retval = PM_RET_OK;

    /* If wrong number of args, raise TypeError */
    if(NATIVE_GET_NUM_ARGS() != 1)
    {
        PM_RAISE(retval, PM_RET_EX_TYPE);
        return retval;
    }

    /* If port is not an unsigned short, raise TypeError */
    pport = NATIVE_GET_LOCAL(0);
    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
    {
        PM_RAISE(retval, PM_RET_EX_TYPE);
        return retval;
    }

    port = ((pPmInt_t)pport)->val & 0xFFFF;

    data = inb(port);

    retval = int_new((int32_t)data, &pdata);
    NATIVE_SET_TOS(pdata);
    return retval;
    """
    pass


#def inw(port):
#    """__NATIVE__
#
#    unsigned short int port;
#    unsigned short int data;
#
#    pPmObj_t pport;
#    pPmObj_t pdata;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 1)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    data = inw(port);
#
#    retval = int_new((int32_t)data, &pdata);
#    NATIVE_SET_TOS(pdata);
#    return retval;
#    """
#    pass
#
#
#
#def inl(port):
#    """__NATIVE__
#
#    unsigned short int port;
#    unsigned int data;
#
#    pPmObj_t pport;
#    pPmObj_t pdata;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 1)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    data = inl(port);
#
#    retval = int_new((int32_t)data, &pdata);
#    NATIVE_SET_TOS(pdata);
#    return retval;
#    """
#    pass
#
#
#def inb_p(port):
#    """__NATIVE__
#
#    unsigned short int port;
#    unsigned char data;
#
#    pPmObj_t pport;
#    pPmObj_t pdata;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 1)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    data = inb_p(port);
#
#    retval = int_new((int32_t)data, &pdata);
#    NATIVE_SET_TOS(pdata);
#    return retval;
#    """
#    pass
#
#
#def inw_p(port):
#    """__NATIVE__
#
#    unsigned short int port;
#    unsigned short int data;
#
#    pPmObj_t pport;
#    pPmObj_t pdata;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 1)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    data = inw_p(port);
#
#    retval = int_new((int32_t)data, &pdata);
#    NATIVE_SET_TOS(pdata);
#    return retval;
#    """
#    pass
#
#
#
#def inl_p(port):
#    """__NATIVE__
#
#    unsigned short int port;
#    unsigned int data;
#
#    pPmObj_t pport;
#    pPmObj_t pdata;
#    PmReturn_t retval = PM_RET_OK;
#
#    /* If wrong number of args, raise TypeError */
#    if(NATIVE_GET_NUM_ARGS() != 1)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    /* If port is not an unsigned short, raise TypeError */
#    pport = NATIVE_GET_LOCAL(0);
#    if(OBJ_GET_TYPE(pport) != OBJ_TYPE_INT)
#    {
#        PM_RAISE(retval, PM_RET_EX_TYPE);
#        return retval;
#    }
#
#    port = ((pPmInt_t)pport)->val & 0xFFFF;
#
#    data = inl_p(port);
#
#    retval = int_new((int32_t)data, &pdata);
#    NATIVE_SET_TOS(pdata);
#    return retval;
#    """
#    pass
